package com.zy.design.pattern.proxy.rpc.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author zhaoyi
 * @date 2021/4/8 16:36
 * 远程服务 基于socket
 * 简单点写就支持者一个方法...
 */
public class ServerInstance {

    private int port;

    public ServerInstance(int port) {
        this.port = port;
    }

    //启动监听
    public void start() throws IOException {

        // 创建服务端socket
        ServerSocket serverSocket = new ServerSocket(port);
        System.out.println("服务端已启动监听: " + port);

        //循环监听等待客户端的连接
        while (true) {
            // 监听客户端
            Socket socket = serverSocket.accept();
            System.out.println("收到请求来自于 " + socket.getInetAddress());
            ServerThread thread = new ServerThread(socket);
            thread.start();
        }

    }


}
